package com.lucky.ebidding.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lucky.ebidding.dto.*;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.FileService;
import com.lucky.ebidding.service.TbService;
import com.lucky.ebidding.service.UserService;
import com.lucky.ebidding.service.ZbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:showZbController
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/3/21 15:50
 * @Author:857251389@qq.com
 */
@Controller
public class ZbController {

    @Autowired
    private ZbService zbService;

    @Autowired
    private TbService tbService;

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    /**
     * 展示招标项目
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/show/{id}")
    public String show(@PathVariable(name = "id") Integer id,
                       Model model,
                       HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");

        ZbInfo zbInfo = zbService.selectById(id);
        try{

            TbInfo tbInfo = tbService.selectTbInfoByIdAndZbId(user.getId(), id);
            if(tbInfo!=null){
                model.addAttribute("tbInfo",tbInfo);
            }
        }catch (Exception e){
            //
        }
        model.addAttribute("zbInfo", zbInfo);
        return "zb/zb_show";
    }

    @GetMapping("/showZb/{id}")
    public String showZb(@PathVariable(name = "id") Integer id,
                         Model model) {

        ZbInfo zbInfo = zbService.selectById(id);
        model.addAttribute("zbInfo",zbInfo);

        return "zb/showzb";
    }



    @ResponseBody
    @GetMapping("/getSh")
    public TbDTO getSh() {

        List<TbInfo> tbInfos = tbService.selectByBedFlag(0);
        ArrayList<TbAndZbIdDTO> tbAndZbIdDTOS = new ArrayList<>();
        for (TbInfo tbInfo : tbInfos) {
            ZbInfo zbInfo = zbService.selectById(tbInfo.getZbId());
            TbAndZbIdDTO tbAndZbIdDTO = new TbAndZbIdDTO();
            tbAndZbIdDTO.setTbInfo(tbInfo);
            tbAndZbIdDTO.setZbInfo(zbInfo);
            tbAndZbIdDTOS.add(tbAndZbIdDTO);
        }
        TbDTO tbDTO = new TbDTO();

        tbDTO.setData(tbAndZbIdDTOS);
        tbDTO.setCode(0);
        tbDTO.setMsg("yes");
        tbDTO.setCount(tbInfos.size());
        return tbDTO;
    }

    @ResponseBody
    @GetMapping("/get/{id}")
    public ZbDTO get(@PathVariable(name = "id") Integer userId) {

        ZbDTO zbDTO = new ZbDTO();
        List<ZbInfo> zbInfos = zbService.selectByUserId(userId);
        zbDTO.setData(zbInfos);
        zbDTO.setCode(0);
        zbDTO.setMsg("yes");
        zbDTO.setCount(zbInfos.size());
        return zbDTO;

    }

    @ResponseBody
    @GetMapping("/get")
    public ZbDTO get() {

        ZbDTO zbDTO = new ZbDTO();
        List<ZbInfo> zbInfos = zbService.selectByShFlag(1);
        zbDTO.setData(zbInfos);
        zbDTO.setCode(0);
        zbDTO.setMsg("yes");
        zbDTO.setCount(zbInfos.size());
        return zbDTO;

    }

    @ResponseBody
    @GetMapping("/shPreview/{fileId}")
    public FileDTO shPreview(@PathVariable(name = "fileId") Integer fileId) {
        FileInfo fileInfo = fileService.selectById(fileId);
        FileDTO fileDTO = new FileDTO();
        fileDTO.setCode(200);
        fileDTO.setMessage("成功");
        fileDTO.setUrl(fileInfo.getFileUrl());
        fileDTO.setFileId(fileInfo.getId());
        return fileDTO;
    }



    @GetMapping("/expertSelectJump/{id}")
    public String expertSelectJump(@PathVariable(name = "id") Integer zbId,
                                   Model model){
        model.addAttribute("zbId",zbId);
        return "admin/expert_select";
    }

    @GetMapping("/expertSelectTable")
    public String expertSelectTable(){

        return "admin/expert_select_table";
    }

    @GetMapping("/scoreDisplay")
    public String scoreDisplay(Model model,HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        List<ZbInfo> zbInfos = zbService.selectZbInfoByUserId(user.getId());
        model.addAttribute("zbInfos",zbInfos);
        return "zb/scoreDisplay";
    }


    @PostMapping("/publish_apply")
    @ResponseBody
    public ResultDTO publish_apply(Integer zbId,Integer tbId){

        int i = zbService.InsertPublishInfo(zbId, tbId);
        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }

    @GetMapping("/addJump")
    public String addJump(HttpServletRequest request,Model model){

        User user = (User) request.getSession().getAttribute("user");
        UserShInfo userShInfo = userService.selectUserShInfoByUserId(user.getId());

        model.addAttribute("userShInfo",userShInfo);
        return "zb/zb_sh_file_add";
    }

    @GetMapping("/zbAddSectionJump")
    public String zbAddSectionJump(HttpServletRequest request,Model model){

        User user = (User) request.getSession().getAttribute("user");
        UserShInfo userShInfo = userService.selectUserShInfoByUserId(user.getId());

        model.addAttribute("userShInfo",userShInfo);
        return "zb/zb_sh_file_add_section";
    }

    @PostMapping("/zb/add")
    public String zbShAdd(UserShInfo userShInfo, HttpServletResponse response) throws IOException {

        userService.updateUserShInfo(userShInfo);

        PrintWriter out = response.getWriter();
        out.print("<script>window.parent.location.href='/zb_bam';</script>");
        out.flush();
        return null;
    }
}
